import apis from "@/http/apis.js";
export default ({
    namespaced: true,
    state: {
        sellers: [],
        pagesData: {
            pageSize: 3,
            currentPage: 1,
            total: 0,
            pages: 0,
        },
        sellerById: [],
        sellersOrders: [],
        sellersUsers: [],

        allClaimsData: [],
        orderId: [],
        orderMes: [],
        search: {
            type: "ordersTime",
            timeData: "",
            nameData: "",
        },
        active:"all"
    },
    getters: {

    },
    mutations: {
        getActive(state,data){
            state.active = data
        },
        getsearch(state, data) {
            state.search = data
        },
        sellersData(state, data) {
            state.sellers = data.data;
            state.pagesData.total = data.total;
            state.pagesData.pages = data.pages;
        },
        changePageSize(state, data) {
            state.pagesData.pageSize = data
            state.pagesData.currentPage = 1

        },
        changeCurrentPage(state, data) {
            state.pagesData.currentPage = data
        },

        sellersOrdersData(state, data) {
            state.sellersOrders = data
            console.log(state.sellersOrders);
        },

        sellersOrdersTotal(state, data) {
            state.pagesData.total = data;
        },

        sellersUsersData(state, data) {
            state.sellersUsers = data.usersData
            state.pagesData.total = data.total;
        },

        allClaimsData(state, data) {
            state.orderId = data.orders
            state.allClaimsData = data.allClaims
            state.pagesData.total = data.total;
        },

        getOrderMes(state, data) {
            state.orderMes = state.orderId.filter(value => value._id == data)[0]
        }
    },

    actions: {
        async getSellers(context, search) {
            const {
                data
            } = await apis.sellers.getSellers({
                ...search,
                ...context.state.pagesData,
            })
            console.log(data);
            context.commit("sellersData", data.data)
        },

        async delSellersById(context, _id) {
            const {
                data
            } = await apis.sellers.delSellersById({
                _id
            })
            if (data.status) {
                if (context.state.sellers.length == 1) {
                    context.commit('changeCurrentPage', context.state.pagesData.currentPage - 1)
                }
                context.dispatch("getSellers")
            }
        },

        async addSellers(context, seller) {
            const { data } = await apis.sellers.addSellers({
                ...seller,
            });
            return data
        },

        async getSellersById(context, _id) {
            const { data } = await apis.sellers.getSellersById({
                _id
            });
            if (data.status) {
                console.log(data.data[0]);
                context.state.sellerById = data.data[0];
            }
        },

        async changeSellersById(context, seller) {
            const { data } = await apis.sellers.changeSellersById({
                ...seller,
            });
            return data
        },

        async getSellersOrderById(context, { _id}) {
            const {
                data
            } = await apis.sellers.getSellersOrderById({
                _id,
                ...context.state.search,
                ...context.state.pagesData,
            })
            console.log(data);
            switch (context.state.active) {
                case "all": {
                    context.commit("sellersOrdersData", data.data.allOrders)
                    context.commit("sellersOrdersTotal", data.data.allTotal)
                    break;
                }
                case "paid": {
                    context.commit("sellersOrdersData", data.data.paidOrders)
                    context.commit("sellersOrdersTotal", data.data.paidTotal)
                    break;
                }
                case "payoff": {
                    context.commit("sellersOrdersData", data.data.payOffOrders)
                    context.commit("sellersOrdersTotal", data.data.payOffTotal)
                    break;
                }
                case "toCanceling": {
                    context.commit("sellersOrdersData", data.data.toCancelingOrders)
                    context.commit("sellersOrdersTotal", data.data.toCancelingTotal)
                    break;
                }
                case "toCanceled": {
                    context.commit("sellersOrdersData", data.data.toCanceledOrders)
                    context.commit("sellersOrdersTotal", data.data.toCanceledTotal)
                    break;
                }
                case "toCancel": {
                    console.log("1");
                    context.commit("sellersOrdersData", data.data.toCancelOrders)
                    context.commit("sellersOrdersTotal", data.data.toCancelTotal)
                    break;
                }
            }
        },

        async getSellersUserById(context, { _id, searchData }) {
            const {
                data
            } = await apis.sellers.getSellersUserById({
                _id,
                ...searchData,
                ...context.state.pagesData,
            })
            console.log(data.data);
            context.commit("sellersUsersData", data.data)
        },

        async getSellersByIdForClaims(context, { _id, searchData }) {
            const {
                data
            } = await apis.sellers.getSellersByIdForClaims({
                _id,
                ...searchData,
                ...context.state.pagesData,
            })
            console.log(data);
            context.commit("allClaimsData", data.data)
        },
    },


    modules: {

    }
})